import baseApi from './baseApi';

// 用户类型定义
export interface User {
  id: number;
  uuid: string;
  username: string;
  mobile: string;
  credit: number;
  balance: number;
}

// 分页参数类型
export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

// 分页响应类型
export interface PaginationResponse {
  page: number;
  pageSize: number;
  total: number;
}

// 用户列表响应
export interface UserListResponse {
  code: number;
  message: string;
  data: {
    pagination: PaginationResponse;
    list: User[];
  };
}

// 用户详情响应
export interface UserDetailResponse {
  code: number;
  message: string;
  data: User;
}

// 余额/积分记录类型
export interface BalanceRecord {
  id: number;
  amount: number;
  createdAt: string;
  updatedAt: string;
}

// 记录列表响应
export interface RecordListResponse {
  code: number;
  message: string;
  data: {
    pagination: PaginationResponse;
    list: BalanceRecord[];
  };
}

// 更新余额请求参数
export interface UpdateBalanceRequest {
  balance: number;
}

// 更新积分请求参数
export interface UpdateCreditRequest {
  credit: number;
}

// 用户列表查询参数
export interface UserListParams extends PaginationParams {
  keyword?: string;
}

class UserService {
  // 获取用户列表
  static async getUserList(params?: UserListParams): Promise<UserListResponse> {
    const response = await baseApi.get('/admin/user/list', { params });
    return response.data;
  }

  // 获取用户详情
  static async getUserDetail(id: number): Promise<UserDetailResponse> {
    const response = await baseApi.get(`/admin/user/${id}`);
    return response.data;
  }

  // 更新用户余额
  static async updateUserBalance(id: number, data: UpdateBalanceRequest): Promise<{ code: number; message: string }> {
    const response = await baseApi.put(`/admin/user/${id}/balance`, data);
    return response.data;
  }

  // 更新用户积分
  static async updateUserCredit(id: number, data: UpdateCreditRequest): Promise<{ code: number; message: string }> {
    const response = await baseApi.put(`/admin/user/${id}/credit`, data);
    return response.data;
  }

  // 获取用户余额变动记录
  static async getUserBalanceRecords(id: number, params?: PaginationParams): Promise<RecordListResponse> {
    const response = await baseApi.get(`/admin/user/${id}/balance/record`, { params });
    return response.data;
  }

  // 获取用户积分变动记录
  static async getUserCreditRecords(id: number, params?: PaginationParams): Promise<RecordListResponse> {
    const response = await baseApi.get(`/admin/user/${id}/credit/record`, { params });
    return response.data;
  }

  // 根据关键词搜索用户
  static async searchUsers(keyword: string, params?: PaginationParams): Promise<UserListResponse> {
    const searchParams = {
      ...params,
      keyword
    };
    const response = await baseApi.get('/admin/user/list', { params: searchParams });
    return response.data;
  }
}

export default UserService; 